'use strict';
angular.module('app.controllers')
  .controller('BudgetCtrl', function($scope, $state, $http, $window, TrService) {
    var vm = this;
    vm.loading = false;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.calculate = function() {
      vm.budget.costytd = parseFloat(vm.budget.costytdorg) - parseFloat(vm.budget.costmtdorg) + parseFloat(vm.budget.costmtd);
      vm.budget.hourytd = parseFloat(vm.budget.hourytdorg) - parseFloat(vm.budget.hourmtdorg) + parseFloat(vm.budget.hourmtd);
      vm.budget.costmtdp = parseFloat(vm.budget.costmtd) / parseFloat(vm.budget.annual) * 100;
      vm.budget.costytdp = parseFloat(vm.budget.costytd) / parseFloat(vm.budget.annual) * 100;
    };

    vm.load = function() {
      vm.loading = true;
      TrService.budgetGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.budget = ret.data.value;
        vm.loading = false;
      })
    }
    vm.load();

    vm.saveTable = function() {
      vm.loading = true;
      if (!vm.budget.annual || vm.budget.annual == '') {
        vm.annual = true;
        vm.loading = false;
        return;
      } else {
        vm.annual = false;
      }
      var sendBody = vm.budget;
      sendBody.year = $window.sessionStorage.year;
      sendBody.month = $window.sessionStorage.month;
      sendBody.hotelId = $window.sessionStorage.hotelId;
      TrService.budgetPost(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading = false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })

    }
  })
  .controller('EmployeeCtrl', function($scope, $state, $window, TrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;
    vm.load = function() {
      vm.loading = true;
      TrService.employeeGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.result = ret.data.value.data;
        vm.loading = false;
      })
    }
    vm.load();
    vm.calculate = function(ret) {
      //ret.target = parseInt(ret.targetOrg) - parseInt(ret.targetMtdOrg) + parseInt(ret.targetMtd);
      ret.ytdP = parseInt(ret.ytd) / parseInt(ret.target) * 100;
    }

    vm.saveTable = function(ret) {
      vm.loading = true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result
      }
      TrService.employeePost(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading = false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })
    }

  })
  .controller('ManagerCtrl', function($scope, $state, $window, TrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;

    vm.load = function() {
      vm.loading = true;
      TrService.managerGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.result = ret.data.value.data;
        vm.loading = false;
      })
    }
    vm.load();
    vm.calculate = function(ret) {
      // ret.ytd = parseInt(ret.ytdOrg) - parseInt(ret.mtdOrg) + parseInt(ret.mtd);
      ret.ytdP = parseInt(ret.ytd) / parseInt(ret.target) * 100;
    }

    vm.saveTable = function(ret) {
      vm.loading = true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result
      }
      TrService.managerPost(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading = false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })
    }

  })
  .controller('CrossCtrl', function($scope, $state, $window, TrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;

    vm.load = function() {
      vm.loading = true;
      TrService.crossGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.result = ret.data.value.data;
        if (!vm.result || vm.result.length == 0) {
          vm.result = [{
            "name": "",
            "depart": "",
            "date": "",
            "hotel": "",
            "reason": ""
          }, {
            "name": "",
            "depart": "",
            "date": "",
            "hotel": "",
            "reason": ""
          }];
        }
        vm.loading = false;
      })
    }
    vm.load();
    vm.rowCheck = function() {
      // var inpute = 0;
      // var all = 0;
      // angular.forEach(vm.result, function(value, key) {
      //   if (value.name != '' || value.depart != '' || value.date != '' || value.reason != '' || value.hotel != '') {
      //     inpute++;
      //   }
      //   all++;
      // })
      // if (inpute == all) {
      //   vm.result.push({
      //     "name": "",
      //     "depart": "",
      //     "date": "",
      //     "jiudian": "",
      //     "reason": ""
      //   })
      // }
    }
    vm.remove = function(list, index) {
      list.splice(index, 1);

    }
    vm.add = function() {
      vm.result.push({
        "name": "",
        "depart": "",
        "date": "",
        "jiudian": "",
        "reason": ""
      })
    }
    vm.saveTable = function(ret) {
      vm.loading = true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result
      }
      TrService.crossPost(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading = false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })
    }
  })
  .controller('OtherCtrl', function($scope, $state, $window, TrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;
    vm.load = function() {
      vm.loading = true;
      TrService.otherGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.result = ret.data.value.data;
        if (!vm.result || vm.result.length == 0) {
          vm.result = [{
            "activity": "",
            "pax": "",
            "date": "",
            "teacher": "",
            "summary": "",
            "reason": ""
          }, {
            "activity": "",
            "pax": "",
            "date": "",
            "teacher": "",
            "summary": "",
            "reason": ""
          }];
        }
        vm.loading = false;
      })
    }
    vm.load();

    vm.rowCheck = function() {
      // var inpute = 0;
      // var all = 0;
      // angular.forEach(vm.result, function(value, key) {
      //   if (value.activity != '' || value.pax != '' || value.date != '' || value.reason != '' || value.teacher != '' || value.summary != '') {
      //     inpute++;
      //   }
      //   all++;
      // })
      // if (inpute == all) {
      //   vm.result.push({
      //     "activity": "",
      //     "pax": "",
      //     "date": "",
      //     "teacher": "",
      //     "summary": "",
      //     "reason": ""
      //   })
      // }
    }
    vm.remove = function(list, index) {
      list.splice(index, 1);

    }
    vm.add = function() {
      vm.result.push({
        "activity": "",
        "pax": "",
        "date": "",
        "teacher": "",
        "summary": "",
        "reason": ""
      })
    }
    vm.saveTable = function(ret) {
      vm.loading = true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result
      }
      TrService.otherPost(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading = false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })
    }
  })
  .controller('ExternalCtrl', function($scope, $state, $window, TrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;
    vm.load = function() {
      vm.loading = true;
      TrService.externalGet($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.result = ret.data.value.data;
        vm.loading = false;
      })
    }
    vm.load();

    vm.saveTable = function(ret) {
      vm.loading = true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result
      }
      TrService.externalPost(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading = false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })
    }

  })
  .controller('DepartCtrl', function($scope, $state, $window, TrService) {
    var vm = this;
    vm.editable = $window.sessionStorage.editable == 'true';
    vm.loading = false;
    vm.load = function() {
      vm.loading = true;
      TrService.depart1Get($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
        vm.result = ret.data.value.data;
        if (!vm.result || vm.result.length == 0) {
          vm.result = [{
            "depart": "",
            "pmtd": 0,
            "dmtd": 0,
            "pax": "",
            "hour": "",
            "pytd": "",
            "dytd": "",
            "reason": ""
          }, {
            "depart": "",
            "pmtd": 0,
            "dmtd": 0,
            "pax": "",
            "hour": "",
            "pytd": "",
            "dytd": "",
            "reason": ""
          }];

        }
        TrService.depart2Get($window.sessionStorage.year, $window.sessionStorage.month, $window.sessionStorage.hotelId).then(function(ret) {
          vm.result2 = ret.data.value.data;
          if (!vm.result2 || vm.result2.length == 0) {
            vm.result2 = [{
              "depart": "",
              "td1": "",
              "td2": "",
              "td3": "",
              "td4": "",
              "td5": "",
              "td6": "",
              "td7": "",
              "td8": "",
              "td9": "",
              "td10": "",
              "td11": "",
              "td12": ""
            }, {
              "depart": "",
              "td1": "",
              "td2": "",
              "td3": "",
              "td4": "",
              "td5": "",
              "td6": "",
              "td7": "",
              "td8": "",
              "td9": "",
              "td10": "",
              "td11": "",
              "td12": ""
            }];
          }
          vm.loading = false;
        })
      })

    }
    vm.load();


    // vm.rowCheck = function() {
    //   var inpute = 0;
    //   var all = 0;
    //   angular.forEach(vm.result, function(value, key) {
    //     if (value.depart != '' || value.pax != '' || value.date != '' || value.reason != '' || value.teacher != '' || value.summary != '') {
    //       inpute++;
    //     }
    //     all++;
    //   })
    //   if (inpute == all) {
    //     vm.result.push({
    //       "depart": "",
    //       "pmtd": 0,
    //       "dmtd": 0,
    //       "pax": "",
    //       "hour": "",
    //       "pytd": "",
    //       "dytd": "",
    //       "reason": ""
    //     })
    //   }
    // }
    vm.remove = function(list, index) {
      list.splice(index, 1);

    }
    vm.add = function() {
      vm.result.push({
        "depart": "",
        "pmtd": 0,
        "dmtd": 0,
        "pax": "",
        "hour": "",
        "pytd": "",
        "dytd": "",
        "reason": ""
      })
    }
    vm.add2 = function() {
      vm.result2.push({
        "depart": "",
        "td1": "",
        "td2": "",
        "td3": "",
        "td4": "",
        "td5": "",
        "td6": "",
        "td7": "",
        "td8": "",
        "td9": "",
        "td10": "",
        "td11": "",
        "td12": ""
      })
    }

    vm.saveTable = function(ret) {
      vm.loading=true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result
      }
      TrService.depart1Post(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg = ret.reason;
          vm.loading=false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg = 'Saved. ' + new Date();
        }
      })
    }

    vm.saveTable2 = function(ret) {
      vm.loading=true;
      var sendBody = {
        year: $window.sessionStorage.year,
        month: $window.sessionStorage.month,
        hotel: $window.sessionStorage.hotelId,
        data: vm.result2
      }
      TrService.depart2Post(sendBody).then(function(ret) {
        if (ret.code == 'FAILED') {
          vm.msg2 = ret.reason;
          vm.loading=false;
          return false;
        } else {
          vm.load();
          $scope.loadProgress();
          vm.msg2 = 'Saved. ' + new Date();
        }
      })
    }

  })